﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using STS.Model;
using System;

namespace STS.ViewModel
{
    public class PoraIloscViewModel : BaseViewModel
    {
        private ObservableCollection<PoraIlosc> _poraIloscItems;
        public ObservableCollection<PoraIlosc> PoraIloscItems
        {
            get
            {
                if (_poraIloscItems == null)
                {
                    _poraIloscItems = new ObservableCollection<PoraIlosc>();
                }
                return _poraIloscItems;
            }
            set
            {
                _poraIloscItems = value;
                NotifyPropertyChanged("PoraIloscItems");
            }
        }

        public void FindPoraIloscItems()
        {
            var poraIloscDB = from PoraIlosc poraIlosc in ModelInstance.DataContext.PoraIloscItems
                              select poraIlosc;
            PoraIloscItems = new ObservableCollection<PoraIlosc>(poraIloscDB);
        }

        public PoraIlosc FindPoraIloscItemById(int poraIloscId)
        {
            var poraIloscDB = from PoraIlosc poraIlosc in ModelInstance.DataContext.PoraIloscItems
                        where poraIlosc.Id == poraIloscId
                        select poraIlosc;
            return poraIloscDB.SingleOrDefault<PoraIlosc>();
        }

        public ObservableCollection<PoraIlosc> FindPoraIloscItemByLek(Lek lek)
        {
            var poraIloscDB = from PoraIlosc poraIlosc in ModelInstance.DataContext.PoraIloscItems
                              where poraIlosc.Lek == lek
                              select poraIlosc;
            ObservableCollection<PoraIlosc> poraIloscItems = new ObservableCollection<PoraIlosc>(poraIloscDB);
            return poraIloscItems;
        }

        public void UpdatePoraIlosc(PoraIlosc updatePoraIlosc)
        {
            PoraIlosc old = FindPoraIloscItemById(updatePoraIlosc.Id);
            SetEntityPropertities(ref old, updatePoraIlosc);
            SaveChangesToDB();
        }

        public void AddPoraIlosc(PoraIlosc poraIlosc)
        {
            ModelInstance.DataContext.PoraIloscItems.InsertOnSubmit(poraIlosc);
            SaveChangesToDB();
            PoraIloscItems.Add(poraIlosc);
        }

        public void RemovePoraIlosc(PoraIlosc poraIlosc)
        {
            PoraIloscItems.Remove(poraIlosc);
            ModelInstance.DataContext.PoraIloscItems.DeleteOnSubmit(poraIlosc);
            SaveChangesToDB();
        }

        public void RemoveAllPoraIlosc()
        {
            var poraIloscDB = from PoraIlosc poraIlosc in ModelInstance.DataContext.PoraIloscItems
                         select poraIlosc;

            foreach (PoraIlosc _poraIlosc in poraIloscDB)
            {
                ModelInstance.DataContext.PoraIloscItems.DeleteOnSubmit(_poraIlosc);
            }

            try
            {
                SaveChangesToDB();
                PoraIloscItems.Clear();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
    }
}
